package io.github.biezhi.elves.examples;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.github.biezhi.elves.Elves;
import io.github.biezhi.elves.common.Constants;
import io.github.biezhi.elves.common.CookieConstants;
import io.github.biezhi.elves.common.MapConstants;
import io.github.biezhi.elves.config.Config;
import io.github.biezhi.elves.model.EbaseInfo;
import io.github.biezhi.elves.model.EnterpriseProfileInfo;
import io.github.biezhi.elves.model.LegalInfo;
import io.github.biezhi.elves.model.NoticeCourt;
import io.github.biezhi.elves.model.RefereeDoc;
import io.github.biezhi.elves.ok.http.FastHttpClient;
import io.github.biezhi.elves.pipeline.Pipeline;
import io.github.biezhi.elves.request.Request;
import io.github.biezhi.elves.response.Response;
import io.github.biezhi.elves.response.Result;
import io.github.biezhi.elves.spider.Spider;
import io.github.biezhi.elves.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

public class GongShangExample {
	@Slf4j
	static class GongShangSpider extends Spider {
		final boolean isPage = false;
		
		final String splitString = ",";
		//处理量
		int handleCount = 0;
		//要爬取企业数据结果量
		int totalHandle = 0;
		//定义数据库企业名称没有匹配到企查查网站数据的量
		int countIsNull = 0;
		static int pageNo = 1;
		final int pageSize = 3;
		
		//定义企查查列表查询可查最大分页数
		static final int maxSearchPageNo = 3;
		
		static String requestParamVal = "";
		static String indexUrl = "http://www.qichacha.com";
		final String contentAsynUrl = "/company_getinfos?";
		final String contentCmsAsynUrl = "/cms_guquanmap";
		static String unique = "";
		static String companyname = "";
		static String[] tabs = new String[]{"base","susong","run","touzi","report","assets","history"};
		static String cookieVal = CookieConstants.QYY_CCA_COOKIE;
		List<EbaseInfo> crawlTableDataList = null;
		List<EbaseInfo> crawlTableDataIsNullList = null;
		
		final String qichachaSearchUrl = "http://www.qichacha.com/search?key=";
		
		final String gongshangUrl = "http://www.qichacha.com";
		@SuppressWarnings("deprecation")
		final String gongshangSearchKeyUrl = "http://www.qichacha.com/search?key=" + URLEncoder.encode("深圳华厦财富基金管理有限公司");
		final String gongshangSearchUrl = "http://www.qichacha.com/g_GD.html";
		@SuppressWarnings("deprecation")
		public GongShangSpider(String name) {
			super(name);
			List<String> crawlTableDataList = queryCrawlTableData();
			if(crawlTableDataList!=null && crawlTableDataList.size()>0){
				int index = 0;
				totalHandle = crawlTableDataList.size();
				StringBuffer searchUrl = new StringBuffer();
				for(String crawlTableData : crawlTableDataList){
					String crawlTableDataUrl = qichachaSearchUrl + URLEncoder.encode(crawlTableData);
					searchUrl.append(crawlTableDataUrl);
					if(index!=(crawlTableDataList.size()-1)){
						searchUrl.append(splitString);
					}
					index++;
				}
				String urls = searchUrl.toString();
				System.out.println(searchUrl.toString());
				if(isPage){
					this.startUrls(gongshangSearchUrl);
				}else{
					this.startUrls(urls.split(splitString));
				}
			}
		}
		
		private List<String> queryCrawlTableData(){
			List<String> strList = new ArrayList<String>();
//			strList.add("江西天人生态股份有限公司");
//			strList.add("深圳市海信汇通国际投资有限公司");
//			strList.add("深圳市富通达车库有限公司福田分公司");
//			strList.add("北京四通投资有限公司");
//			strList.add("山西省焦炭集团有限责任公司");
//			strList.add("深圳市罗湖区投资管理公司");
//			strList.add("惠州市富强大酒店");
			strList.add("0177");
			return strList;
		}
		
		@Override
		public void onStart(Config config) {
			this.addPipeline((Pipeline<List<String>>) (item, request) -> log.info("保存到文件: {}", item));
		}

		/**
		 * 爬取企业数据
		 */
		@SuppressWarnings({ "unchecked", "rawtypes" })
		public Result parse(Response response) {
			if(isPage){
				//企业列表分页
				return parseEntnamePage(response);
			}else{
				//根据企业名称精确匹配数据
				return parseSearchByEntname(response);
			}
		}
		
		/**
		 * 企业列表分页
		 * @param response
		 * @return
		 */
		@SuppressWarnings("rawtypes")
		private Result<List<String>> parseEntnamePage(Response response){
			Result<List<String>> result = new Result<List<String>>();
			Elements elements = response.body().css(".row .col-md-12 section.panel-default a");
			if(elements!=null && elements.size()>0){
				for(Element element : elements){
					String title = element.select("span.clear > span.name").text();
					if(title==null || "".equals(title)){
						continue;
					}
					String titleUrl = element.attr("href");
					System.out.println("title==>" + title);
					System.out.println("href==>" + titleUrl);
					
					String clickTitleUrl = gongshangUrl + titleUrl;
					clickTitleUrl = "http://www.qichacha.com/firm_4359198d09bf3237ae0465f6d40ef43a.html";
					
					Request clickTitleReq = this.makeRequest(clickTitleUrl, this::parseContent);
					result.addRequest(clickTitleReq);
					break;
				}
				List<String> titles = elements.stream().map(Element::text).collect(Collectors.toList());
				System.out.println("titles:" + titles);
				result.setItem(titles);
	
				// 获取下一页 URL
				Elements nextEl = response.body().css(".row > div.col-md-12 > div.text-left > ul.pagination > li > a.next");
				if (null != nextEl && nextEl.size() > 0) {
					String nextPageUrl = gongshangUrl + nextEl.get(0).attr("href");
					System.out.println("nextPageUrl{{}}==>" + nextPageUrl);
					Request nextReq = this.makeRequest(nextPageUrl, this::parseEntnamePage);
					result.addRequest(nextReq);
				}
			}else{
				try {
					System.out.println("休眠10000毫秒！！！");
					Thread.sleep(10000);
					System.out.println("休眠后，再次调用" + this.getClass().getName());
					parse(response);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return result;
		}
		
		/**
		 * 根据企业名称精确匹配数据
		 */
		@SuppressWarnings({ "deprecation", "rawtypes" })
		public Result<List<String>> parseSearchByEntname(Response response) {
			String requestUrl = response.getRequest().getUrl();
			System.out.println(this.getClass().getName() + "{{}}==>" + requestUrl);
			System.out.println(response.body());
			
			if(requestUrl.indexOf("key=")!=-1){
				requestParamVal = requestUrl.substring(requestUrl.indexOf("key=")+4);
				requestParamVal = URLDecoder.decode(requestParamVal);
				System.out.println("requestParamVal:" + requestParamVal);
			}
			
			int pageIndex = 0;
			boolean needNextPage = true;
			Result<List<String>> result = new Result<List<String>>();
			Elements elements = response.body().css(".row .col-md-9 #ajaxlist section.panel > table > tbody > tr");
			if(elements!=null && elements.size()>0){
				for(Element element : elements){
					String clickTitleUrl = "";
					String entname = element.select("td").get(1).select("a.ma_h1").text();
					if((entname==null || "".equals(entname)) || !requestParamVal.equals(entname)){
						pageIndex++;
						continue;
					}else{
						needNextPage = false;
//						String legalName = element.select("p.m-t-xs > a.text-primary").text();
						String titleUrl = element.select("td").get(1).select("a").attr("href");
						String jyStatus = element.select("td").get(2).select("span.nstatus").text();
						System.out.println("entname==>" + entname);
						System.out.println("href==>" + titleUrl);
						System.out.println("jyStatus==>" + jyStatus);
						
						clickTitleUrl = indexUrl + titleUrl;
//						clickTitleUrl = "http://www.qichacha.com/firm_4359198d09bf3237ae0465f6d40ef43a.html";
						if(clickTitleUrl.indexOf("?")==-1){
							clickTitleUrl += "?companyname="+URLEncoder.encode(entname);
						}else{
							if(clickTitleUrl.indexOf("?")!=-1){
								clickTitleUrl += "&companyname="+URLEncoder.encode(entname);
							}
						}
						
						Request clickTitleReq = this.makeRequest(clickTitleUrl, this::parseContentAsynGetUrl);
						result.addRequest(clickTitleReq);
						break;
					}
				}
			}else{
				try {
					System.out.println("休眠10000毫秒！！！");
					Thread.sleep(10000);
					System.out.println("休眠后，再次调用" + this.getClass().getName());
					parse(response);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			if(needNextPage && (elements!=null && pageIndex==elements.size())){
				// 获取下一页 URL
				Elements nextEl = response.body().css("#ajaxlist > div > .m-t-lg > ul.pagination > li > a");
				if(nextEl==null || nextEl.size()==0){
					nextEl = response.body().css("div > .m-t-lg > ul.pagination > li > a");
				}
				if (null != nextEl && nextEl.size() > 0) {
					try{
						String href = "";
						String hrefText = "";
						for(Element pageElement : nextEl){
							hrefText = pageElement.select("a").text();
							if(">".equals(hrefText)){
								href = pageElement.select("#ajaxpage").attr("href");
								break;
							}else{
								continue;
							}
						}
						if(">".equals(hrefText)){
							String nextPageNoStr = href.replace("javascript:getSearchPage(", "").replace(")", "");
							nextPageNoStr = nextPageNoStr.substring(0,nextPageNoStr.indexOf(","));
							int nextPageNo = Integer.parseInt(nextPageNoStr);
							String nextPageUrl = indexUrl + "/search_index?key=" + requestParamVal + "&ajaxflag=1&p="+ nextPageNo + "&";
							//返回响应url内容
							String responseBody = getHttpReponseBody(nextPageUrl);
							//企查查列表查询每页内容
							parseSearchContentPage(nextPageUrl, responseBody);
						}
					}catch(Exception e){
						System.out.println("企查查列表查询下一页数据获取异常{{}}==>" + e.getMessage());
					}
				}
			}
			return result;
		}
		
		/**
		 * 获取企查查列表查询每页内容
		 * @param requestUrl
		 * @param responseBodyContent
		 * @return
		 */
		@SuppressWarnings({ "rawtypes", "deprecation" })
		public Result<List<String>> parseSearchContentPage(String requestUrl,String responseBodyContent){
			System.out.println(this.getClass() + ".parseSearchContentPage{{}}==>" + requestUrl);
//			System.out.println(this.getClass() + ".parseSearchContentPage{{}}==>" + responseBodyContent);
			
			int pageNo = 0;
			String keyEntname = "";
			Map<String,Object> urlParamMap = null;
			try{
				urlParamMap = StringUtils.urlParamToMap(requestUrl);
			}catch(Exception e){
				System.out.println(this.getClass().getName() + ",urlParamMap获取URL参数异常{{}}==>" + e.getMessage());
			}
			
			if(urlParamMap!=null && urlParamMap.containsKey("key")){
				keyEntname = StringUtils.getValueForKey("key", urlParamMap);
			}
			if(urlParamMap!=null && urlParamMap.containsKey("p")){
				pageNo = StringUtils.getIntValueForKey("p", urlParamMap);
			}
			
			Result<List<String>> result = new Result<List<String>>();
			Document doc = Jsoup.parse(responseBodyContent);
			
			int pageIndex = 1;
			boolean needNextPage = true;
			Elements elements = doc.select(".row .col-md-9 #ajaxlist section.panel > table > tbody > tr");
			if(elements==null || elements.size()==0){
				elements = doc.select("#searchlist > table > tbody > tr");
			}
			if(elements!=null && elements.size()>0){
				for(Element element : elements){
					String clickTitleUrl = "";
					String entname = element.select("td").get(1).select("a.ma_h1").text();
					if((entname==null || "".equals(entname)) || !keyEntname.equals(entname)){
						pageIndex++;
						continue;
					}else{
						needNextPage = false;
//						String legalName = element.select("p.m-t-xs > a.text-primary").text();
						String titleUrl = element.select("td").get(1).select("a").attr("href");
						String jyStatus = element.select("td").get(2).select("span.nstatus").text();
						System.out.println("entname==>" + entname);
						System.out.println("href==>" + titleUrl);
						System.out.println("jyStatus==>" + jyStatus);
						
						clickTitleUrl = indexUrl + titleUrl;
						if(clickTitleUrl.indexOf("?")==-1){
							clickTitleUrl += "?companyname="+URLEncoder.encode(entname);
						}else{
							if(clickTitleUrl.indexOf("?")!=-1){
								clickTitleUrl += "&companyname="+URLEncoder.encode(entname);
							}
						}
						
						Request clickTitleReq = this.makeRequest(clickTitleUrl, this::parseContentAsynGetUrl);
						result.addRequest(clickTitleReq);
						break;
					}
				}
			}
			
			if(pageNo < maxSearchPageNo){
				if(needNextPage && (elements!=null && pageIndex==elements.size())){
					// 获取下一页 URL
					Elements nextEl = doc.select("#ajaxlist > div > .m-t-lg > ul.pagination > li > a");
					if(nextEl==null || nextEl.size()==0){
						nextEl = doc.select("div > .m-t-lg > ul.pagination > li > a");
					}
					if (null != nextEl && nextEl.size() > 0) {
						try{
							String href = "";
							String hrefText = "";
							for(Element pageElement : nextEl){
								hrefText = pageElement.select("a").text();
								if(">".equals(hrefText)){
									href = pageElement.select("#ajaxpage").attr("href");
									break;
								}else{
									continue;
								}
							}
							if(">".equals(hrefText)){
								String nextPageNoStr = href.replace("javascript:getSearchPage(", "").replace(")", "");
								nextPageNoStr = nextPageNoStr.substring(0,nextPageNoStr.indexOf(","));
								int nextPageNo = Integer.parseInt(nextPageNoStr);
								String nextPageUrl = indexUrl + indexUrl + "/search_index?key=" + keyEntname + "&ajaxflag=1&p="+ nextPageNo + "&";
								System.out.println("第" + pageNo + "页，searchPageNextPageUrl{{}}==>" + nextPageUrl);
								//返回响应url内容
								String responseBody = getHttpReponseBody(nextPageUrl);
								//企查查列表查询每页内容
								parseSearchContentPage(nextPageUrl, responseBody);
							}
						}catch(Exception e){
							System.out.println("企查查列表查询下一页数据获取异常{{}}==>" + e.getMessage());
						}
					}
				}
			}
			return result;
		}
		
		/**
		 * 获取内容页异步Url
		 * @param response
		 * @return
		 */
		@SuppressWarnings({ "rawtypes", "unused", "deprecation" })
		public Result parseContentAsynGetUrl(Response response) {
			Result<List<String>> result = new Result<List<String>>();
			String requestUrl = response.getRequest().getUrl();
			System.out.println(this.getClass().getName() + "==>" + requestUrl);
			
			Map<String,Object> urlParamMap = null;
			try{
				urlParamMap = StringUtils.urlParamToMap(requestUrl);
			}catch(Exception e){
				System.out.println(this.getClass().getName() + "，urlParamMap获取URL参数异常{{}}==>" + e.getMessage());
			}
			Document doc = Jsoup.parse(response.body().toString());
			Element inputElement = doc.getElementById("unique");
			unique = inputElement.attr("value");
			
			//根据企业名称查询法人代表信息表unique_id字段
			LegalInfo legalInfo = null;//staticShareholderService.queryEntnameByUnique(unique);
			if(legalInfo!=null){
				companyname = legalInfo.getEntname();
				companyname = URLEncoder.encode(companyname);
			}else{
				if(urlParamMap!=null){
					companyname = StringUtils.getValueForKey("companyname", urlParamMap);
				}
			}
			System.out.println(this.getClass().getName() + ",unique:" + unique);
			String baseUrl = indexUrl + contentAsynUrl + "unique="+unique+"&companyname="+companyname+"&tab="+tabs[0];
			Request clickTitleReq = this.makeRequest(baseUrl, this::parseContent);
			result.addRequest(clickTitleReq);
			return result;
		}
		
		/**
		 * 获取内容
		 * @param response
		 * @return
		 */
		@SuppressWarnings({ "rawtypes", "unused" })
		public Result parseContent(Response response) {
			String requestUrl = response.getRequest().getUrl();
			System.out.println(this.getClass().getName() + "{{}}==>" + requestUrl);
//			System.out.println(response.body());
			
			Result<List<String>> result = new Result<List<String>>();
			
			Elements elementsPt = null;//response.body().css("div.p-t");
			if(elementsPt==null){
//				List<String> titlesPt = elementsPt.stream().map(Element::text).collect(Collectors.toList());
//				for(String titlePt : titlesPt){
//					System.out.println("titlePt:" + titlePt);
//				}
//				System.out.println("titlesPt:" + titlesPt);
//				result.setItem(titlesPt);
				
				//工商信息
				Elements elementsCominfo = response.body().css("div.m-t-md .row .col-md-9 .data_div_login #Cominfo");
				if(elementsCominfo!=null && elementsCominfo.size()>0){
					//法定代表人名称
					String cominfoBname = elementsCominfo.select("table.ntable > tbody > tr > td > div.boss-td > div.clearfix > div.pull-left > a.bname").text();
					System.out.println("cominfoBname:" + cominfoBname);
				}
				
				//股东信息
//				Elements elementsSockinfo = response.body().css("div.m-t-md .row .col-md-9 .data_div_login #Sockinfo > table > tbody > tr");
//				if(elementsSockinfo!=null && elementsSockinfo.size()>0){
//					List<String> titlesSockinfo = elementsSockinfo.stream().map(Element::text).collect(Collectors.toList());
//					System.out.println("titlesSockinfo:" + titlesSockinfo);
//					result.setItem(titlesSockinfo);
//				}
				
				//主要人员
//				Elements elementsMainmember = response.body().css("div.m-t-md .row .col-md-9 .data_div_login #Mainmember > table > tbody > tr");
//				if(elementsMainmember!=null && elementsMainmember.size()>0){
//					List<String> titlesMainmember = elementsMainmember.stream().map(Element::text).collect(Collectors.toList());
//					for(String titleMainmember : titlesMainmember){
//						System.out.println("titleMainmember:" + titleMainmember);
//						for(String titleMainmemberStr : titleMainmember.split(splitString)){
//							System.out.println("titleMainmemberStr:" + titleMainmemberStr);
//						}
//					}
//					System.out.println("titlesMainmember:" + titlesMainmember);
//					result.setItem(titlesMainmember);
//				}
				
				//变更记录
//				Elements elementsChangelist = response.body().css("div.m-t-md .row .col-md-9 .data_div_login #Changelist > table > tbody > tr");
//				if(elementsChangelist!=null && elementsChangelist.size()>0){
//					List<String> titlesChangelist = elementsChangelist.stream().map(Element::text).collect(Collectors.toList());
//					for(String titleChangelist : titlesChangelist){
//						System.out.println("titleChangelist:" + titleChangelist);
//						for(String titleChangelistStr : titleChangelist.split(splitString)){
//							System.out.println("titleChangelistStr:" + titleChangelistStr);
//						}
//					}
//					System.out.println("titlesChangelist:" + titlesChangelist);
//					result.setItem(titlesChangelist);
//				}
				
				handleCount++;
				
				int companyNavCount = 0;
				String contentUrl = indexUrl + "/firm_" + unique + ".html";
				//返回响应url内容
				String responseBody = getHttpReponseBody(contentUrl);
				Document doc = Jsoup.parse(responseBody);
				Elements elementsCompanyNav = doc.select("#company-nav > .nav-tabs");
				String companyNavCountStr = elementsCompanyNav.select("li").get(1).select("a > span").text();
				if(!StringUtils.isNullOrEmpty(companyNavCountStr)){
					companyNavCount = Integer.parseInt(companyNavCountStr);
				}
				
				if(companyNavCount>0){
					//判断处理量与要处理数据量是否相等，相等为已处理完本页的爬取数据并成功入库
					if(handleCount==totalHandle){
						//法律诉讼
						String baseUrl = indexUrl + contentAsynUrl + "unique="+unique+"&companyname="+companyname+"&tab="+tabs[1];
						Request clickTitleReq = this.makeRequest(baseUrl, this::parseContentSusong);
						result.addRequest(clickTitleReq);
					}
				}else{
					//判断处理量与要处理数据量是否相等，相等为已处理完本页的爬取数据并成功入库
					if(handleCount==totalHandle){
						System.out.println("已经全部爬取数据处理完，程序自动停止！！！");
						System.exit(0);
					}
				}
			}else{
				try {
					System.out.println("休眠10000毫秒！！！");
					Thread.sleep(10000);
					System.out.println("休眠后，再次调用" + this.getClass().getName());
					parse(response);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return result;
		}
		
		/**
		 * 获取法律诉讼内容
		 * @param response
		 * @return
		 */
		@SuppressWarnings({ "rawtypes", "unused" })
		public Result parseContentSusong(Response response){
			String requestUrl = response.getRequest().getUrl();
			System.out.println(this.getClass().getName() + "{{}}==>" + requestUrl);
			
			String unique = "";
			Map<String,Object> urlParamMap = null;
			try{
				urlParamMap = StringUtils.urlParamToMap(requestUrl);
			}catch(Exception e){
				System.out.println(this.getClass().getName() + ",urlParamMap获取URL参数异常{{}}==>" + e.getMessage());
			}
			if(urlParamMap!=null && urlParamMap.containsKey("unique")){
				unique = StringUtils.getValueForKey("unique", urlParamMap);
			}
			Result<List<String>> result = new Result<List<String>>();
			
			//被执行人
//			Elements elementsZhixinglist = response.body().css("div.m-t-md .row .col-md-9 #susong_div > #zhixinglist > table > tbody > tr");
//			if(elementsZhixinglist==null || elementsZhixinglist.size()==0){
//				elementsZhixinglist = response.body().css("section#zhixinglist > table > tbody > tr");
//			}
//			if(elementsZhixinglist!=null && elementsZhixinglist.size()>0){
//				for(Element element : elementsZhixinglist){
//					if(element.select("th").size()>0 || element.select("td").size()==0){
//						continue;
//					}
//					Map<String,Object> zhixingMap = new HashMap<String,Object>();
//					
//					//案号
//					zhixingMap.put("caseNo", getReplaceStr(element.select("td").get(1).text()));
//					//立案时间
//					zhixingMap.put("filingTime", getReplaceStr(element.select("td").get(2).text()));
//					//执行法院
//					zhixingMap.put("courtExecution", getReplaceStr(element.select("td").get(3).text()));
//					//执行标的
//					zhixingMap.put("executes", getReplaceStr(element.select("td").get(4).text()));
//					
//					try{
//						List<EbaseInfo> ebaseInfoList = null;//staticShareholderService.queryEbaseInfoByEntname(companyname);
//						if(ebaseInfoList!=null && ebaseInfoList.size()>0){
//							for(EbaseInfo ebaseInfo : ebaseInfoList){
//								zhixingMap.put("uniqueId", unique);
//								zhixingMap.put("pripid", ebaseInfo.getPripid());
//								//转换json字符串
//								String susongJson = JSON.toJSONString(zhixingMap);
//								ExecutorInfo executorInfo = JSON.parseObject(susongJson,ExecutorInfo.class);
//								System.out.println(JSON.toJSON(executorInfo));
////								staticShareholderService.saveExecutorInfo(executorInfo);
//							}
//						}else{
//							try{
//								zhixingMap.put("uniqueId", unique);
//								//转换json字符串
//								String susongJson = JSON.toJSONString(zhixingMap);
//								ExecutorInfo executorInfo = JSON.parseObject(susongJson,ExecutorInfo.class);
//								System.out.println(JSON.toJSON(executorInfo));
////								staticShareholderService.saveExecutorInfo(executorInfo);
//							}catch(Exception e){
//								System.out.println("保存法律诉讼被执行人信息异常==>" + e.getMessage());
//							}
//						}
//					}catch(Exception e){
//						System.out.println("保存法律诉讼被执行人或查询企业异常==>" + e.getMessage());
//					}
//				}
//			}
			
			//裁判文书
			parseWenshuContent(response);
			
			//法院公告
			Elements elementsGonggaolist = response.body().css("div.m-t-md .row .col-md-9 #susong_div > #gonggaolist > table > tbody > tr");
			if(elementsGonggaolist==null || elementsGonggaolist.size()==0){
				elementsGonggaolist = response.body().css("section#gonggaolist > table > tbody > tr");
			}
			if(elementsGonggaolist!=null && elementsGonggaolist.size()>0){
				for(Element element : elementsGonggaolist){
					if(element.select("th").size()>0 || element.select("td").size()==0){
						continue;
					}
					Map<String,Object> gonggaoMap = new HashMap<String,Object>();
					
					//当事人
					gonggaoMap.put("party", getReplaceStr(element.select("td").get(1).text()));
					//公告类型
					gonggaoMap.put("noticeType", getReplaceStr(element.select("td").get(2).text()));
					//公告人
					gonggaoMap.put("noticeMan", getReplaceStr(element.select("td").get(3).text()));
					//刊登时间
					gonggaoMap.put("publishTime", getReplaceStr(element.select("td").get(4).text()));
					
					try{
						List<EbaseInfo> ebaseInfoList = null;//staticShareholderService.queryEbaseInfoByEntname(companyname);
						if(ebaseInfoList!=null && ebaseInfoList.size()>0){
							for(EbaseInfo ebaseInfo : ebaseInfoList){
								gonggaoMap.put("uniqueId", unique);
								gonggaoMap.put("pripid", ebaseInfo.getPripid());
								//转换json字符串
								String gonggaoJson = JSON.toJSONString(gonggaoMap);
								NoticeCourt noticeCourt = JSON.parseObject(gonggaoJson,NoticeCourt.class);
								System.out.println(JSON.toJSON(noticeCourt));
//								staticShareholderService.saveCourtNotice(noticeCourt);
							}
						}else{
							try{
								gonggaoMap.put("uniqueId", unique);
								//转换json字符串
								String gonggaoJson = JSON.toJSONString(gonggaoMap);
								NoticeCourt noticeCourt = JSON.parseObject(gonggaoJson,NoticeCourt.class);
								System.out.println(JSON.toJSON(noticeCourt));
//								staticShareholderService.saveCourtNotice(noticeCourt);
							}catch(Exception e){
								System.out.println("保存法律诉讼法院公告信息异常==>" + e.getMessage());
							}
						}
					}catch(Exception e){
						System.out.println("保存法律诉讼法院公告或查询企业异常==>" + e.getMessage());
					}
				}
			}
			
			//开庭公告
//			Elements elementsNoticelist = response.body().css("div.m-t-md .row .col-md-9 #susong_div > #noticelist > table > tbody > tr");
//			if(elementsNoticelist==null || elementsNoticelist.size()==0){
//				elementsNoticelist = response.body().css("section#noticelist > table > tbody > tr");
//			}
//			if(elementsNoticelist!=null && elementsNoticelist.size()>0){
//				for(Element element : elementsNoticelist){
//					if(element.select("th").size()>0 || element.select("td").size()==0){
//						continue;
//					}
//					Map<String,Object> noticeMap = new HashMap<String,Object>();
//					
//					//案号
//					noticeMap.put("caseNo", getReplaceStr(element.select("td > a").get(0).text()));
//					//开庭日期
//					noticeMap.put("courtDate", getReplaceStr(element.select("td").get(2).text()));
//					//案由
//					noticeMap.put("causeAction", getReplaceStr(element.select("td").get(3).text()));
//					//公诉人/原告/上诉人/申请人
//					noticeMap.put("plaintiff", getReplaceStr(element.select("td").get(4).text()));
//					//被告人/被告/被上诉人/被申请人
//					noticeMap.put("defendant", getReplaceStr(element.select("td").get(5).text()));
//					
//					try{
//						List<EbaseInfo> ebaseInfoList = null;//staticShareholderService.queryEbaseInfoByEntname(companyname);
//						if(ebaseInfoList!=null && ebaseInfoList.size()>0){
//							for(EbaseInfo ebaseInfo : ebaseInfoList){
//								noticeMap.put("uniqueId", unique);
//								noticeMap.put("pripid", ebaseInfo.getPripid());
//								//转换json字符串
//								String noticeJson = JSON.toJSONString(noticeMap);
//								BulletinCourt bulletinCourt = JSON.parseObject(noticeJson,BulletinCourt.class);
//								System.out.println(JSON.toJSON(bulletinCourt));
////								staticShareholderService.saveCourtNotice(bulletinCourt);
//							}
//						}else{
//							try{
//								noticeMap.put("uniqueId", unique);
//								//转换json字符串
//								String noticeJson = JSON.toJSONString(noticeMap);
//								BulletinCourt bulletinCourt = JSON.parseObject(noticeJson,BulletinCourt.class);
//								System.out.println(JSON.toJSON(bulletinCourt));
////								staticShareholderService.saveCourtNotice(bulletinCourt);
//							}catch(Exception e){
//								System.out.println("保存法律诉讼开庭公告信息异常==>" + e.getMessage());
//							}
//						}
//					}catch(Exception e){
//						System.out.println("保存法律诉讼开庭公告或查询企业异常==>" + e.getMessage());
//					}
//				}
//			}
			return result;
		}
		
		/**
		 * 获取裁判文书内容(分页)
		 * @param response
		 * @return
		 */
		@SuppressWarnings({ "rawtypes", "unused" })
		private Result parseWenshuContent(Response response){
			String requestUrl = response.getRequest().getUrl();
			System.out.println(this.getClass().getName() + "{{}}==>" + requestUrl);
			
			String unique = "";
			String companyname = "";
			Map<String,Object> urlParamMap = null;
			try{
				urlParamMap = StringUtils.urlParamToMap(requestUrl);
			}catch(Exception e){
				System.out.println(this.getClass().getName() + ",urlParamMap获取URL参数异常{{}}==>" + e.getMessage());
			}
			if(urlParamMap!=null && urlParamMap.containsKey("unique")){
				unique = StringUtils.getValueForKey("unique", urlParamMap);
			}
			if(urlParamMap!=null && urlParamMap.containsKey("companyname")){
				companyname = StringUtils.getValueForKey("companyname", urlParamMap);
			}
			
			Result<List<String>> result = new Result<List<String>>();
//			System.out.println(this.getClass().getName() + "{{}}==>" + response.body());
			
			Elements elementsWenshulist = response.body().css("div.m-t-md .row .col-md-9 #susong_div > #wenshulist > table > tbody > tr");
			if(elementsWenshulist==null || elementsWenshulist.size()==0){
				elementsWenshulist = response.body().css("section#wenshulist > table > tbody > tr");
			}
			if(elementsWenshulist!=null && elementsWenshulist.size()>0){
				for(Element element : elementsWenshulist){
					if(element.select("th").size()>0 || element.select("td").size()==0){
						continue;
					}
					Map<String,Object> wenshuMap = new HashMap<String,Object>();
					
					//案件名称
					wenshuMap.put("caseName", getReplaceStr(element.select("td > a").get(0).text()));
					//发布时间
					wenshuMap.put("releaseTime", getReplaceStr(element.select("td").get(2).text()));
					//案件编号
					wenshuMap.put("caseNo", getReplaceStr(element.select("td").get(3).text()));
					//案件身份
					wenshuMap.put("caseIdentity", getReplaceStr(element.select("td").get(4).text()));
					//执行法院
					wenshuMap.put("courtExecution", getReplaceStr(element.select("td").get(5).text()));
					
					try{
						List<EbaseInfo> ebaseInfoList = null;//staticShareholderService.queryEbaseInfoByEntname(companyname);
						if(ebaseInfoList!=null && ebaseInfoList.size()>0){
							for(EbaseInfo ebaseInfo : ebaseInfoList){
								wenshuMap.put("uniqueId", unique);
								wenshuMap.put("pripid", ebaseInfo.getPripid());
								//转换json字符串
								String wenshuJson = JSON.toJSONString(wenshuMap);
								RefereeDoc refereeDoc = JSON.parseObject(wenshuJson,RefereeDoc.class);
								System.out.println(JSON.toJSON(refereeDoc));
//								staticShareholderService.saveRefereeDoc(RefereeDoc);
							}
						}else{
							try{
								wenshuMap.put("uniqueId", unique);
								//转换json字符串
								String wenshuJson = JSON.toJSONString(wenshuMap);
								RefereeDoc refereeDoc = JSON.parseObject(wenshuJson,RefereeDoc.class);
								System.out.println(JSON.toJSON(refereeDoc));
//								staticShareholderService.saveRefereeDoc(RefereeDoc);
							}catch(Exception e){
								System.out.println("保存法律诉讼被裁判文书信息异常==>" + e.getMessage());
							}
						}
					}catch(Exception e){
						System.out.println("保存法律诉讼裁判文书或查询企业异常==>" + e.getMessage());
					}
				}
				
				// 获取下一页 URL
				Elements nextEl = response.body().css("#wenshulist > div > .text-right > ul.pagination > li > a");
				if (null != nextEl && nextEl.size() > 0) {
					try{
						String href = nextEl.last().getElementById("ajaxpage").attr("href");
						String nextPageNoStr = href.replace("javascript:getTabList(", "").replace("\"susong\",\"wenshu\")", "").replace(",", "");
						int nextPageNo = Integer.parseInt(nextPageNoStr);
						String nextPageUrl = indexUrl + contentAsynUrl + "unique="+unique+"&companyname="+companyname + "&p=" +nextPageNo +"&tab="+tabs[1] + "&box=wenshu";
						System.out.println("nextPageUrl{{}}==>" + nextPageUrl);
						//返回响应url内容
						String responseBody = getHttpReponseBody(nextPageUrl);
						parseWenshuContentPage(nextPageUrl, responseBody);
						
//						Request nextReq = this.makeRequest(nextPageUrl, this::parseWenshuContent);
//						result.addRequest(nextReq);
					}catch(Exception e){
						System.out.println("裁判文书下一页数据获取异常{{}}==>" + e.getMessage());
					}
				}
			}
			return result;
		}
		
		/**
		 * 获取裁判文书每页内容
		 * @param response
		 * @return
		 */
		@SuppressWarnings({ "rawtypes", "unused" })
		private Result parseWenshuContentPage(String requestUrl,String responseBodyContent){
			System.out.println(this.getClass().getName() + "{{}}==>" + requestUrl);
			
			String unique = "";
			String companyname = "";
			Map<String,Object> urlParamMap = null;
			try{
				urlParamMap = StringUtils.urlParamToMap(requestUrl);
			}catch(Exception e){
				System.out.println(this.getClass().getName() + ",urlParamMap获取URL参数异常{{}}==>" + e.getMessage());
			}
			if(urlParamMap!=null && urlParamMap.containsKey("unique")){
				unique = StringUtils.getValueForKey("unique", urlParamMap);
			}
			if(urlParamMap!=null && urlParamMap.containsKey("companyname")){
				companyname = StringUtils.getValueForKey("companyname", urlParamMap);
			}
			
			Result<List<String>> result = new Result<List<String>>();
			Document doc = Jsoup.parse(responseBodyContent);
			System.out.println(this.getClass().getName() + "{{}}==>" + responseBodyContent);
			
//			Elements elementsWenshulist = response.body().css("div.m-t-md .row .col-md-9 #susong_div > #wenshulist > table > tbody > tr");
			Elements elementsWenshulist = doc.select(".row .col-md-9 #ajaxlist section.panel > table > tbody > tr > td");
			if(elementsWenshulist==null || elementsWenshulist.size()==0){
				elementsWenshulist = doc.select("section#wenshulist > table > tbody > tr");
			}
			if(elementsWenshulist==null || elementsWenshulist.size()==0){
				elementsWenshulist = doc.select("table.ntable > tbody > tr");
			}
			if(elementsWenshulist!=null && elementsWenshulist.size()>0){
				for(Element element : elementsWenshulist){
					if(element.select("th").size()>0 || element.select("td").size()==0){
						continue;
					}
					Map<String,Object> wenshuMap = new HashMap<String,Object>();
					
					//案件名称
					wenshuMap.put("caseName", getReplaceStr(element.select("td > a").get(0).text()));
					//发布时间
					wenshuMap.put("releaseTime", getReplaceStr(element.select("td").get(2).text()));
					//案件编号
					wenshuMap.put("caseNo", getReplaceStr(element.select("td").get(3).text()));
					//案件身份
					wenshuMap.put("caseIdentity", getReplaceStr(element.select("td").get(4).text()));
					//执行法院
					wenshuMap.put("courtExecution", getReplaceStr(element.select("td").get(5).text()));
					
					try{
						List<EbaseInfo> ebaseInfoList = null;//staticShareholderService.queryEbaseInfoByEntname(companyname);
						if(ebaseInfoList!=null && ebaseInfoList.size()>0){
							for(EbaseInfo ebaseInfo : ebaseInfoList){
								wenshuMap.put("uniqueId", unique);
								wenshuMap.put("pripid", ebaseInfo.getPripid());
								//转换json字符串
								String wenshuJson = JSON.toJSONString(wenshuMap);
								RefereeDoc refereeDoc = JSON.parseObject(wenshuJson,RefereeDoc.class);
								System.out.println(JSON.toJSON(refereeDoc));
//								staticShareholderService.saveRefereeDoc(refereeDoc);
							}
						}else{
							try{
								wenshuMap.put("uniqueId", unique);
								//转换json字符串
								String wenshuJson = JSON.toJSONString(wenshuMap);
								RefereeDoc refereeDoc = JSON.parseObject(wenshuJson,RefereeDoc.class);
								System.out.println(JSON.toJSON(refereeDoc));
//								staticShareholderService.saveRefereeDoc(refereeDoc);
							}catch(Exception e){
								System.out.println("保存法律诉讼被裁判文书信息异常==>" + e.getMessage());
							}
						}
					}catch(Exception e){
						System.out.println("保存法律诉讼裁判文书或查询企业异常==>" + e.getMessage());
					}
				}
				
				// 获取下一页 URL
				Elements nextEl = doc.select("#wenshulist > div > .text-right > ul.pagination > li > a");
				if(nextEl==null || nextEl.size()==0){
					nextEl = doc.select("div > .text-right > ul.pagination > li > a");
				}
				if (null != nextEl && nextEl.size() > 0) {
					try{
						String hrefText = nextEl.last().getElementById("ajaxpage").text();
						if(">".equals(hrefText)){
							String href = nextEl.last().getElementById("ajaxpage").attr("href");
							String nextPageNoStr = href.replace("javascript:getTabList(", "").replace("\"susong\",\"wenshu\")", "").replace(",", "");
							int nextPageNo = Integer.parseInt(nextPageNoStr);
							String nextPageUrl = indexUrl + contentAsynUrl + "unique="+unique+"&companyname="+companyname + "&p=" +nextPageNo +"&tab="+tabs[1] + "&box=wenshu";
							System.out.println("nextPageUrl{{}}==>" + nextPageUrl);
							//返回响应url内容
							String responseBody = getHttpReponseBody(nextPageUrl);
							parseWenshuContentPage(nextPageUrl, responseBody);
						}
					}catch(Exception e){
						System.out.println("裁判文书下一页数据获取异常{{}}==>" + e.getMessage());
					}
				}
			}
			return result;
		}
		
		/**
		 * 替换指定字符并返回处理后结果
		 * @param strVal
		 * @return
		 */
		private String getReplaceStr(String strVal){
			if(!StringUtils.isNullOrEmpty(strVal)){
				if(Constants.REPLACE_HENG_STR.equals(strVal)){
					strVal = strVal.replace(Constants.REPLACE_HENG_STR, "");
					return strVal;
				}
			}
			return strVal;
		}
		
		/**
		 * 返回响应url内容
		 * @return
		 */
		private String getHttpReponseBody(String requestUrl){
			String jsonStr = "";
			String proxyIp = "210.38.1.131";
			int proxyPort = 80;
			
			proxyIp = "132.122.1.4";
			proxyPort = 9898;
			
			Map<String,Object> proxyIpMap = StringUtils.getHttpProxyIp(1);
			String proxyJson = JSONObject.toJSONString(proxyIpMap);
			System.out.println(proxyJson);
			proxyIp = StringUtils.getValueForKey("proxyHost", proxyIpMap);
			proxyPort = StringUtils.getIntValueForKey("proxyPort", proxyIpMap);
			try {
				io.github.biezhi.elves.ok.http.Response okHttpResponse;
				if(!Constants.IS_PROXY){
					okHttpResponse = FastHttpClient
							.get()
							.addHeader("cookie", CookieConstants.QYY_CCA_COOKIE)
							.url(requestUrl)
							.build()
							.execute();
				}else{
					String cookie = "zg_did=%7B%22did%22%3A%20%221614a3e94d675-0dd0f0f64d8a19-393d5d0e-1fa400-1614a3e94d74%22%7D; UM_distinctid=1614a3e953792-0c4f5aee0c9974-393d5d0e-1fa400-1614a3e95380; _uab_collina=151736907666208657786343; PHPSESSID=thelop48butne1aidcdp0ernk2; CNZZDATA1254842228=535638187-1517367375-%7C1526004663; Hm_lvt_3456bee468c83cc63fb5147f119f1075=1524903460,1524904134,1525224768,1526009457; hasShow=1; acw_tc=AQAAANturFHITQMAgrpsca17Oza+3PUq; _umdata=486B7B12C6AA95F2A4F682D6C31223F0986ECB48B6B5E1D6BE7EA5816E9CFF9D1E467C2930EA264FCD43AD3E795C914C1EC528AEDE58186B1AC70293CE853116; Hm_lpvt_3456bee468c83cc63fb5147f119f1075=1526009480; zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f=%7B%22sid%22%3A%201526009456301%2C%22updated%22%3A%201526009827148%2C%22info%22%3A%201526009456311%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%2C%22cuid%22%3A%20%223810af54a3ad5fded928c79ee9b4159c%22%7D";
					Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp, proxyPort));
					okHttpResponse = FastHttpClient
							.newBuilder()
							.proxy(proxy)
							.build()
							.get().addHeader("cookie", cookie)
							.url(requestUrl)
							.build()
							.execute();
				}
				jsonStr = okHttpResponse.string();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonStr;
		}
		
		/**
		 * 返回响应url内容
		 * @return
		 */
		private String getHttpPostReponseBody(String requestUrl){
			String jsonStr = "";
			String proxyIp = "210.38.1.131";
			int proxyPort = 80;
			
			proxyIp = "132.122.1.4";
			proxyPort = 9898;
			
			try {
				io.github.biezhi.elves.ok.http.Response okHttpResponse;
				if(!Constants.IS_PROXY){
					okHttpResponse = FastHttpClient
							.post()
							.addHeader("cookie", CookieConstants.QYY_CCA_COOKIE)
							.url(requestUrl)
							.build()
							.execute();
				}else{
					Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp, proxyPort));
					okHttpResponse = FastHttpClient
							.newBuilder()
							.proxy(proxy)
							.build()
							.post().addHeader("Content-Type", "application/json; charset=UTF-8")
							.url(requestUrl)
							.build()
							.execute();
				}
				jsonStr = okHttpResponse.string();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonStr;
		}
	}
	
	/**
	 * 解析json数据并保存企业图谱数据入库
	 * @param cmsAsynUrl
	 * @param jsonStr
	 * @param unique
	 * @param entname
	 * @param requestEntnameVal
	 */
	private static void saveEnterpriseProfileData(String cmsAsynUrl,String jsonStr,String unique,String entname,String requestEntnameVal){
		String resultJson = "";
		String parentKeyNo = "";
		boolean isChildren = false;
		GongShangSpider gongShangSpider = new GongShangSpider("广东工商企业");
		try {
			JSONObject json = JSONObject.parseObject(jsonStr);
			if(json.containsKey("Result")){
				resultJson = json.getString("Result");
				
				JSONObject nodeJson = JSONObject.parseObject(resultJson);
				if(nodeJson.containsKey("Node")){
					resultJson = nodeJson.getString("Node");
				}
			}else{
				resultJson = json.getString("children");
			}
			JSONObject resultJsonObject = JSONObject.parseObject(resultJson);
			EnterpriseProfileInfo enterpriseProfileInfo = null;
			List<EbaseInfo> ebaseInfoList = null;//staticShareholderService.queryEbaseInfoByEntname(requestEntnameVal);
			if(ebaseInfoList!=null && ebaseInfoList.size()>0){
				for(EbaseInfo ebaseInfo : ebaseInfoList){
					enterpriseProfileInfo = JSON.parseObject(resultJson, EnterpriseProfileInfo.class);
					
					if(!StringUtils.isNullOrEmpty(unique)){
						enterpriseProfileInfo.setUniqueId(unique);
					}
					enterpriseProfileInfo.setPripid(ebaseInfo.getPripid());
					//企业名称
					if(resultJsonObject.containsKey("name")){
						enterpriseProfileInfo.setEntname(resultJsonObject.getString("name"));
					}
					if(!isChildren){
						if(resultJsonObject.containsKey("KeyNo")){
							parentKeyNo = resultJsonObject.getString("KeyNo");
						}else{
							parentKeyNo = getKeyNoUUID();
						}
						enterpriseProfileInfo.setParentKeyNo("0");
					}else{
						enterpriseProfileInfo.setParentKeyNo(parentKeyNo);
					}
					System.out.println("非循环体{{}}==>json转换对象数据{{}}==>" + enterpriseProfileInfo);
					//保存企业图谱
//					staticShareholderService.saveEnterpriseProfileInfo(enterpriseProfileInfo);
				}
			}else{
				enterpriseProfileInfo = JSON.parseObject(resultJson, EnterpriseProfileInfo.class);
				
				if(!StringUtils.isNullOrEmpty(unique)){
					enterpriseProfileInfo.setUniqueId(unique);
				}
				//企业名称
				if(resultJsonObject.containsKey("name")){
					enterpriseProfileInfo.setEntname(resultJsonObject.getString("name"));
				}
				if(!isChildren){
					if(resultJsonObject.containsKey("KeyNo")){
						parentKeyNo = resultJsonObject.getString("KeyNo");
					}else{
						parentKeyNo = getKeyNoUUID();
					}
					enterpriseProfileInfo.setParentKeyNo("0");
				}else{
					enterpriseProfileInfo.setParentKeyNo(parentKeyNo);
				}
				System.out.println("非循环体{{}}==>json转换对象数据{{}}==>" + enterpriseProfileInfo);
				//保存企业图谱数据
//				staticShareholderService.saveEnterpriseProfileInfo(enterpriseProfileInfo);
			}
			
			String childrenKeyNo = "";
			String childrenParentKeyNo = "";
			if(resultJsonObject.containsKey("children") && resultJsonObject.get("children")!=null){
				JSONArray array = resultJsonObject.getJSONArray("children");
				for (int i = 0; i < array.size(); i++) {
					JSONObject arrJsonObject = array.getJSONObject(i);
					String arrJsonStr = arrJsonObject.toJSONString();
					JSONObject childrenJsonObject = JSONObject.parseObject(arrJsonStr);
					enterpriseProfileInfo = JSON.parseObject(arrJsonStr, EnterpriseProfileInfo.class);
					if(!StringUtils.isNullOrEmpty(unique)){
						enterpriseProfileInfo.setUniqueId(unique);
					}
					//企业名称
					if(childrenJsonObject.containsKey("name")){
						enterpriseProfileInfo.setEntname(childrenJsonObject.getString("name"));
					}
					if(!childrenJsonObject.containsKey("KeyNo")){
						childrenKeyNo = getKeyNoUUID();
						childrenParentKeyNo = childrenKeyNo;
						enterpriseProfileInfo.setKeyNo(childrenKeyNo);
						enterpriseProfileInfo.setParentKeyNo(parentKeyNo);
					}else{
						childrenParentKeyNo = childrenJsonObject.getString("KeyNo");
						enterpriseProfileInfo.setParentKeyNo(parentKeyNo);
					}
					System.out.println("循环体{{}}==>json转换对象数据{{}}==>" + enterpriseProfileInfo);
					//保存企业图谱数据
//					staticShareholderService.saveEnterpriseProfileInfo(enterpriseProfileInfo);
				    if(arrJsonObject.containsKey("children") && (arrJsonObject.get("children")!=null || !"null".equals(arrJsonObject.get("children").toString()))){
				    	//解析企业图谱子json数据
				    	saveEnterpriseProfileChildrenData(arrJsonObject.get("children").toString(),unique,entname,childrenParentKeyNo);
				    }
				}
			}
		} catch (Exception e) {
			System.err.println("io.github.biezhi.elves.spider.saveEnterpriseProfileData，解析json数据并保存企业图谱数据入库异常，cmsAsynUrl链接为:" + cmsAsynUrl + "，json串为:" + jsonStr + "，异常信息{{}}==>" + StringUtils.getErrorInfoFromException(e));
			
			System.out.println("io.github.biezhi.elves.spider.saveEnterpriseProfileData，休眠5000毫秒后，重新再调用......");
			try {
				Thread.sleep(5000);
				System.out.println("io.github.biezhi.elves.spider.saveEnterpriseProfileData，已休眠5000毫秒，开始重新再调用saveEnterpriseProfileData方法===>");
				System.err.println("企业名称{{}}==>" + entname);
				//返回响应url内容
				jsonStr = gongShangSpider.getHttpReponseBody(cmsAsynUrl);
				saveEnterpriseProfileData(cmsAsynUrl,jsonStr, unique, entname, requestEntnameVal);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 解析企业图谱子json数据
	 * @param jsonStr
	 * @param unique
	 * @param entname
	 * @param childrenParentKeyNo
	 */
	private static void saveEnterpriseProfileChildrenData(String jsonStr,String unique,String entname,String childrenParentKeyNo){
		try{
			JSONArray array = JSONArray.parseArray(jsonStr);
			if(array!=null){
				for (int i = 0; i < array.size(); i++) {
					JSONObject arrJsonObject = array.getJSONObject(i);
					String arrJsonStr = arrJsonObject.toJSONString();
					JSONObject childrenJsonObject = JSONObject.parseObject(arrJsonStr);
					
					EnterpriseProfileInfo enterpriseProfileInfo = JSON.parseObject(arrJsonStr, EnterpriseProfileInfo.class);
					if(!StringUtils.isNullOrEmpty(unique)){
						enterpriseProfileInfo.setUniqueId(unique);
					}
					//企业名称
					if(childrenJsonObject.containsKey("name")){
						enterpriseProfileInfo.setEntname(childrenJsonObject.getString("name"));
					}
					if(!childrenJsonObject.containsKey("KeyNo")){
						String childrenKeyNo = getKeyNoUUID();
						enterpriseProfileInfo.setKeyNo(childrenKeyNo);
						enterpriseProfileInfo.setParentKeyNo(childrenParentKeyNo);
					}else{
						enterpriseProfileInfo.setParentKeyNo(childrenParentKeyNo);
					}
					System.out.println("子循环体{{}}==>json转换对象数据{{}}==>" + enterpriseProfileInfo);
					try {
						//保存企业图谱数据
//						staticShareholderService.saveEnterpriseProfileInfo(enterpriseProfileInfo);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}catch(Exception e){
			System.err.println("io.github.biezhi.elves.spider.saveEnterpriseProfileData，解析子json数据并保存企业图谱数据入库异常，json串为:" + jsonStr + "{{}}==>" + StringUtils.getErrorInfoFromException(e));
			
			System.out.println("io.github.biezhi.elves.spider.saveEnterpriseProfileData，休眠5000毫秒后，重新再调用......");
			try {
				Thread.sleep(5000);
				
				System.out.println("io.github.biezhi.elves.spider.saveEnterpriseProfileData，已休眠5000毫秒，开始重新再调用saveEnterpriseProfileData方法===>");
				saveEnterpriseProfileChildrenData(jsonStr, unique, entname, childrenParentKeyNo);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取KeyNo的uuid
	 * @return
	 */
	private static String getKeyNoUUID(){
		//获取uuid
		String uuid = StringUtils.get32UUID();
		while(true){
			//将uuid作为入参，查询企业图谱表数据keyno值，是否已存在数据库中，如果存在，则重新获取，不存在，则直接返回
			int existCount = 0;
			if(existCount>0){
				uuid = "";
			}else{
				break;
			}
		}
		return uuid;
	}
	
	public static void main(String[] args) {
		boolean isSpider = false;
		GongShangSpider gongShangSpider = new GongShangSpider("广东工商企业");
		if(isSpider){
			Elves.me(gongShangSpider, Config.me()).start();
		}else{
			String requestUrl = "http://www.qichacha.com/cms_guquanmap?keyNo=2041bbf946e9d30928f376dfd37526d9";
//			requestUrl = "http://www.qichacha.com/cms_guquanmap?keyNo=h00fa5b09003ea8d4636fb94a6fc3fac";
			//企业图谱
			requestUrl = "http://www.qichacha.com/cms_businessmap?keyNo=7fdcb727cb3c74a0f56ececc59cd9566";
//			requestUrl = "http://www.qichacha.com/cms_businessmap?keyNo=61f80506fff361814eb39a1e86f4d27c";
			requestUrl = "http://www.qichacha.com/cms_businessmap?keyNo=14f64b289717fb0bc6a4a44efffc7d15";
			//投资族谱
			requestUrl = StringUtils.getValueForKey("cms_map", MapConstants.QCC_REQUEST_URL_MAP) + "277b9cc7bc66245fbc26f08bf7362df3";
			//关联图谱
			requestUrl = StringUtils.getValueForKey("company_muhouAction", MapConstants.QCC_REQUEST_URL_MAP) + "277b9cc7bc66245fbc26f08bf7362df3";
			//股权结构图
//			requestUrl = StringUtils.getValueForKey("cms_guquanmap2", MapConstants.QCC_REQUEST_URL_MAP) + "277b9cc7bc66245fbc26f08bf7362df3";
			
			String jsonStr = gongShangSpider.getHttpReponseBody(requestUrl);
			JSONObject json = JSONObject.parseObject(jsonStr);
			System.out.println(json);
//			saveEnterpriseProfileData(requestUrl, jsonStr, "61f80506fff361814eb39a1e86f4d27c", "", "");
			
			
//			requestUrl = "https://132.121.82.93:8080/locationevent-web/auth/key.do?username=3ec8910603ea1dea&pwd=ed3ef9bf0033337830d6c3b1fc132017";
//			requestUrl = "http://132.121.82.93:8080/locationevent-web/api/service/locationconf.do?appkey=0684431bda1bbd74afe47290e1891a21696e2a451a7135f06f55e5456dc6cd17d4b2a68513a8aeb1&methodParam=%7B%22BRANCH_NAME%22%3A+%22%E7%95%AA%E7%A6%BA%E5%88%86%E5%85%AC%E5%8F%B8%22%2C%22REGION_NAME%22%3A+%22%E7%95%AA%E7%A6%BA%E5%A4%A7%E7%9F%B3%E5%AF%8C%E4%B8%BD%E5%BA%97500%22%2C%22CITY_ID%22%3A+%22200%22%2C%22REGION_TYPE%22%3A+%22%E7%BD%91%E7%82%B9%22%2C%22SITE_LIST%22%3A+%5B%7B%22SITE_NAME%22%3A+%22%E7%95%AA%E7%A6%BA%E5%A4%A7%E7%9F%B3%E5%AF%8C%E4%B8%BD%E7%A4%BE%E5%8C%BA%E5%BA%97%22%2C%22SITE_CITY_ID%22%3A+%22200%22%2C%22LAC_CELL_LIST%22%3A+%5B%7B%22CELL_ID%22%3A+%229DD1%22%2C%22LAC_ID%22%3A+%220882%22%7D%2C%7B%22CELL_ID%22%3A+%2220D2%22%2C%22LAC_ID%22%3A+%220882%22%7D%2C%7B%22CELL_ID%22%3A+%2220D0%22%2C%22LAC_ID%22%3A+%220882%22%7D%2C%7B%22CELL_ID%22%3A+%2220D1%22%2C%22LAC_ID%22%3A+%220882%22%7D%2C%7B%22CELL_ID%22%3A+%229DD2%22%2C%22LAC_ID%22%3A+%220882%22%7D%2C%7B%22CELL_ID%22%3A+%229DD0%22%2C%22LAC_ID%22%3A+%220882%22%7D%5D%2C%22SITE_ADDRESS%22%3A+%22%E5%B9%BF%E5%B7%9E%E5%B8%82%E7%95%AA%E7%A6%BA%E5%8C%BA%E5%A4%A7%E7%9F%B3%E8%A1%97%E9%81%93%E5%8A%9E%E6%9C%9D%E9%98%B3%E4%B8%9C%E8%B7%AF60%E5%8F%B7%22%7D%5D%2C%22REMARK%22%3A+%22%22%2C%22REGION_CODE%22%3A+%22200PYWD20180124002%22%7D&methodName=regionconf";
//			String jsonStr = gongShangSpider.getHttpPostReponseBody(requestUrl);
//			JSONObject json = JSONObject.parseObject(jsonStr);
//			System.out.println(json);
			
		}
	}
}